package com.buildings.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.buildings.R;

/***
 * 对话框工具类
 *
 * @author CRay
 */
public class DialogUtils {
  /***
   * 刷新Dialog
   *
   * @param mContext
   * @return
   */
  public static Dialog showRefreshDialog(Context mContext) {
    Dialog dialog = new Dialog(mContext, R.style.monitor_dialog);
    View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_refresh, null);
    dialog.setContentView(view);
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
    return dialog;
  }

  /***
   * 图片上传中...
   * @param context
   * @return
   */
  public static Dialog showUploadProgress(Context context) {
    Dialog dialog = new Dialog(context);
    // 修改对话框默认背景为透明，因为不这么设置的话，对话框默认是白色的
    // 然后你自定义的背景是黑色且有圆角，相信我，你不想看到的
    Window window = dialog.getWindow();
    if (window != null) window.setBackgroundDrawableResource(android.R.color.transparent);
    // 设置自己编写的布局文件，即刚才有 ProgressBar 和 TextView 的那个布局文件
    dialog.setContentView(R.layout.dialog_progress_hint);
    // 设置不可点击或点按返回键取消对话框，这样相当于禁止了用户与界面的交互
    // 实际情况根据需求而定
    dialog.setCancelable(false);

    dialog.show();
    return dialog;
  }

  /***
   * 房屋类型对话框Dialog
   */
  public static Dialog showBuildSelectDialog(Context mContext, final OnClickListener clickListener,
      boolean isTouchClose) {
    final Dialog dialog = new Dialog(mContext, R.style.dialog_anim);
    View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_building_type_select, null);
    dialog.setContentView(view);

    view.findViewById(R.id.tv_town_house).setOnClickListener(new OnClickListener() {
      @Override public void onClick(View view) {
        view.setTag(1);
        clickListener.onClick(view);
        dialog.dismiss();
      }
    });
    view.findViewById(R.id.tv_town_no_house).setOnClickListener(new OnClickListener() {
      @Override public void onClick(View view) {
        view.setTag(2);
        clickListener.onClick(view);
        dialog.dismiss();
      }
    });
    view.findViewById(R.id.tv_village_house).setOnClickListener(new OnClickListener() {
      @Override public void onClick(View view) {
        view.setTag(3);
        clickListener.onClick(view);
        dialog.dismiss();
      }
    });
    view.findViewById(R.id.tv_village_public_build).setOnClickListener(new OnClickListener() {
      @Override public void onClick(View view) {
        view.setTag(4);
        clickListener.onClick(view);
        dialog.dismiss();
      }
    });

    dialog.setCanceledOnTouchOutside(isTouchClose);
    if (dialog != null) {
      dialog.show();
    }
    return dialog;
  }

  /***
   * 加固对话框Dialog
   */
  public static Dialog showReinforceDialog(Context mContext, final OnClickListener clickListener,
      boolean isTouchClose) {
    final Dialog dialog = new Dialog(mContext, R.style.dialog_anim);
    View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_reinforce_layout, null);
    dialog.setContentView(view);

    view.findViewById(R.id.tv_town_house).setOnClickListener(new OnClickListener() {
      @Override public void onClick(View view) {
        view.setTag(1);
        clickListener.onClick(view);
        dialog.dismiss();
      }
    });
    view.findViewById(R.id.tv_town_no_house).setOnClickListener(new OnClickListener() {
      @Override public void onClick(View view) {
        view.setTag(2);
        clickListener.onClick(view);
        dialog.dismiss();
      }
    });

    dialog.setCanceledOnTouchOutside(isTouchClose);
    if (dialog != null) {
      dialog.show();
    }
    return dialog;
  }

  /***
   * 仿ios对话框提示 取消，确定
   *
   * @param mContext
   * @return
   */
  public static Dialog showIOSDialog(Context mContext, String title, String btnMessage1,
      String color1, String btnMessage2, String color2, final OnClickListener cl1,
      final OnClickListener cl2, String content) {
    final Dialog dialog = new Dialog(mContext, R.style.dialog_anim);
    View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_ios, null);
    RelativeLayout rl_content = view.findViewById(R.id.rl_content);
    TextView tvTitle = view.findViewById(R.id.tv_title);
    if (TextUtils.isEmpty(title)) {
      tvTitle.setVisibility(View.GONE);
    } else {
      tvTitle.setVisibility(View.VISIBLE);
      tvTitle.setText(title);
    }
    TextView tvContent = view.findViewById(R.id.tv_content);
    if (TextUtils.isEmpty(content)) {
      tvContent.setVisibility(View.GONE);
    } else {
      tvContent.setVisibility(View.VISIBLE);
      tvContent.setText(content);
    }
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rl_content.getLayoutParams();
    lp.width = (int) (0.72 * mContext.getResources().getDisplayMetrics().widthPixels);
    lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    rl_content.setLayoutParams(lp);
    dialog.setContentView(view);
    TextView tvCancel = view.findViewById(R.id.tv_cancel);
    if (!TextUtils.isEmpty(btnMessage1)) {
      tvCancel.setText(btnMessage1);
    }
    if (!TextUtils.isEmpty(color1)) {
      tvCancel.setTextColor(Color.parseColor(color1));
    }
    tvCancel.setOnClickListener(new OnClickListener() {
      @Override public void onClick(View arg0) {
        if (cl1 != null) {
          cl1.onClick(arg0);
        }
        dialog.dismiss();
      }
    });
    TextView tvCommit = view.findViewById(R.id.tv_commit);
    if (!TextUtils.isEmpty(btnMessage2)) {
      tvCommit.setText(btnMessage2);
    }
    if (!TextUtils.isEmpty(color2)) {
      tvCommit.setTextColor(Color.parseColor(color2));
    }
    tvCommit.setOnClickListener(new OnClickListener() {
      @Override public void onClick(View arg0) {
        if (cl2 != null) {
          cl2.onClick(arg0);
        }
        dialog.dismiss();
      }
    });
    dialog.setCanceledOnTouchOutside(true);
    if (dialog != null) {
      dialog.show();
    }
    return dialog;
  }
}
